#include "./app_adc.h"
#include "./app_flash.h"

struct adc_dev_t *analog_device;//adc 
float VolTemp        = 0;
float VolVin         = 0;
float VolVout        = 0;
float VolIout        = 0;
float pre_VolIout    = 0;//前一次电流值
float VolPerCharge   = 0;

/**
 * @brief 参数结构体，在app_flash 中实现instante
 * 
 */
struct vol_fator_t *vin_fator;
struct vol_fator_t *vout_fator;
struct vol_fator_t *iout_fator;
struct vol_fator_t *tmep_fator;
struct vol_fator_t *precharge_fator;



void app_adc_init(void)
{
    adc_init(&analog_device);
    analog_device->get_factor(analog_device,ANALOG_FACTOR_NUM_TEMP     ,&tmep_fator);
    analog_device->get_factor(analog_device,ANALOG_FACTOR_NUM_VIN      ,&vin_fator);
    analog_device->get_factor(analog_device,ANALOG_FACTOR_NUM_VOUT     ,&vout_fator);
    analog_device->get_factor(analog_device,ANALOG_FACTOR_NUM_IOUT     ,&iout_fator);
    analog_device->get_factor(analog_device,ANALOG_FACTOR_NUM_PRECHARGE,&precharge_fator);
    //读取flash中比值
    app_load_adc_fator(ANALOG_FACTOR_NUM_TEMP     ,tmep_fator);
    app_load_adc_fator(ANALOG_FACTOR_NUM_VIN      ,vin_fator);
    app_load_adc_fator(ANALOG_FACTOR_NUM_VOUT     ,vout_fator);
    app_load_adc_fator(ANALOG_FACTOR_NUM_IOUT     ,iout_fator);
    app_load_adc_fator(ANALOG_FACTOR_NUM_PRECHARGE,precharge_fator);	

}

void app_adc_loop_func(void)
{
    /*手动转换*/
    analog_device->convert_update();  
    /*get adc value*/
    if((analog_device->convert_to_complete(analog_device))==1)//转换完成
    {
   #if 1
        VolTemp      =  analog_device->get_factor_voltage(analog_device, ANALOG_FACTOR_NUM_TEMP     ); 
        VolVin       =  analog_device->get_factor_voltage(analog_device, ANALOG_FACTOR_NUM_VIN      ); 
        VolVout      =  analog_device->get_factor_voltage(analog_device, ANALOG_FACTOR_NUM_VOUT     ); 
        VolIout      =  analog_device->get_factor_voltage(analog_device, ANALOG_FACTOR_NUM_IOUT     ); 
        VolPerCharge =  analog_device->get_factor_voltage(analog_device, ANALOG_FACTOR_NUM_PRECHARGE); 
    #endif
    }
}
void app_adc_device_power(void)
{
	uint16_t i=0xff;
	while(i--)
	{
		app_adc_loop_func();
		
	}
}